#pragma once

#include <iostream>
#include <cstdio>
#include <pthread.h>


typedef void*(*fun_t)(void*);//函数指针

class ThreadData
{
public:
    void *args_;
    std::string name_;
};

//对线程进行封装
class Thread
{
private:
    std::string _name; //线程名字
    fun_t _func;       //线程例程
    ThreadData _tdata; //线程例程的参数
    pthread_t _tid;    //线程id

public:
    Thread(int num,fun_t callback,void* args):_func(callback)
    {
        char nameBuffer[64];
        snprintf(nameBuffer,sizeof(nameBuffer),"Thread-%d",num);
        _name = nameBuffer;

        _tdata.args_ = args;
        _tdata.name_ = _name;
    }

    //创建线程
    void start()
    {
        pthread_create(&_tid,nullptr,_func,(void*)&_tdata);
    }

    //等待线程
    void join()
    {
        pthread_join(_tid,nullptr);
    }
    

    std::string name()
    {
        return _name;
    }

    ~Thread()
    {

    }
};